﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autofac;
using Autofac.Integration.Mvc;
using OperaHouse.DAL.Infrastructure;
using System.Web.Mvc;
using System.Reflection;
using OperaHouse.Repository.Users;

namespace OperaHouse.Web.Framework
{
    public class ComponentRegistrar
    {
        public static void RegisterComponents(string webAssembly)
        {
            var builder = new ContainerBuilder();
            
            builder.RegisterType<UnitOfWork>().As<IUnitOfWork>().InstancePerHttpRequest();
            builder.RegisterType<DatabaseFactory>().As<IDatabaseFactory>().InstancePerHttpRequest();
            builder.RegisterGeneric(typeof(Repository<>)).As(typeof(IRepository<>)).InstancePerHttpRequest();
            builder.RegisterAssemblyTypes(typeof(UserRepository).Assembly)
                .Where(t => t.Name.EndsWith("Repository"))
                .AsImplementedInterfaces().InstancePerHttpRequest(); 
            builder.RegisterControllers(Assembly.Load(webAssembly));
            builder.RegisterAssemblyTypes(Assembly.Load("OperaHouse.Business")).AsImplementedInterfaces();
            
            var container = builder.Build();
            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
        }
    }
}
